package com.wrx.club_management_backend.utils;

import com.wrx.club_management_backend.dto.UserDTO;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.handler.Handler;

/**
 * 登录状态拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1. 获取用户
        UserDTO user = UserHolder.getUser();
        if (user == null || user.getId() == null){
            //2. 未授权，拦截
            response.setStatus(401); // 未授权
            return false;
        }
        //3. 已授权，放行
        return true;
    }
}
